import type { AxiosOperator } from '@/common/infrastructure/axios_operator.ts'
import type { AuthorityInfo } from '@/common/model/app_server/outputs/authority_info'
import type { PageModel } from '@/common/model/app/page_model'
import type { MenuInfo } from '@/common/model/app_server/outputs/menu_info'
import type { RoleInfo } from '@/common/model/app_server/outputs/role_info'

export class AuthorizationApi {
  // 请求操作者
  private _requestOperator: AxiosOperator

  // 请求路由
  private controller: string = '/Authorization'

  /**
   * 有参构造方法
   * @param requestOperator 请求操作者
   * */
  constructor(requestOperator: AxiosOperator) {
    this._requestOperator = requestOperator
  }

  //region # 创建权限 —— createAuthority(authorityName: string...)
  /**
   * 创建权限
   * @param authorityName 权限名称
   * @param authorityPath 权限路径
   * @param description 描述
   * */
  public async createAuthority(authorityName: string, authorityPath: string, description: string) {
    await this._requestOperator.post(`${this.controller}/CreateAuthority`, {
      authorityName,
      authorityPath,
      description,
    })
  }
  //endregion

  //region # 修改权限 —— updateAuthority(authorityId: string...)
  /**
   * 修改权限
   * @param authorityId 权限Id
   * @param authorityName 权限名称
   * @param authorityPath 权限路径
   * @param description 描述
   * */
  public async updateAuthority(
    authorityId: string,
    authorityName: string,
    authorityPath: string,
    description: string,
  ) {
    await this._requestOperator.post(`${this.controller}/UpdateAuthority`, {
      authorityId,
      authorityName,
      authorityPath,
      description,
    })
  }
  //endregion

  //region # 删除权限 —— removeAuthority(authorityId: string)
  /**
   * 删除权限
   * @param authorityId 权限Id
   * */
  public async removeAuthority(authorityId: string) {
    await this._requestOperator.post(`${this.controller}/RemoveAuthority`, {
      authorityId,
    })
  }
  //endregion

  //region # 创建菜单 —— createMenu(menuName: string...)
  /**
   * 创建菜单
   * @param menuName 菜单名称
   * @param sort 排序
   * @param url 链接地址
   * @param path 路径
   * @param icon 图标
   * @param parentNodeId 上级节点Id
   * */
  public async createMenu(
    menuName: string,
    sort: number,
    url: string,
    path: string,
    icon: string,
    parentNodeId: string,
  ) {
    await this._requestOperator.post(`${this.controller}/CreateMenu`, {
      menuName,
      sort,
      url,
      path,
      icon,
      parentNodeId,
    })
  }
  //endregion

  //region # 修改菜单 —— updateMenu(menuId: string...)
  /**
   * 修改菜单
   * @param menuId 菜单Id
   * @param menuName 菜单名称
   * @param sort 排序
   * @param url 链接地址
   * @param path 路径
   * @param icon 图标
   * */
  public async updateMenu(
    menuId: string,
    menuName: string,
    sort: number,
    url: string,
    path: string,
    icon: string,
  ) {
    await this._requestOperator.post(`${this.controller}/UpdateMenu`, {
      menuId,
      menuName,
      sort,
      url,
      path,
      icon,
    })
  }
  //endregion

  //region # 删除菜单 —— removeMenu(menuId: string...)
  /**
   * 删除菜单
   * @param menuId 菜单Id
   * */
  public async removeMenu(menuId: string) {
    await this._requestOperator.post(`${this.controller}/RemoveMenu`, {
      menuId,
    })
  }
  //endregion

  //region # 关联权限到菜单 —— relateAuthoritiesToMenu(menuId: string...)
  /**
   * 关联权限到菜单
   * @param menuId 菜单Id
   * @param authorityIds 权限Id集
   * */
  public async relateAuthoritiesToMenu(menuId: string, authorityIds: Array<string>) {
    await this._requestOperator.post(`${this.controller}/RelateAuthoritiesToMenu`, {
      menuId,
      authorityIds,
    })
  }
  //endregion

  //region # 创建角色 —— createRole(roleName: string...)
  /**
   * 创建角色
   * @param roleName 角色名称
   * @param description 描述
   * @param authorityIds 权限Id集
   * */
  public async createRole(roleName: string, description: string, authorityIds: Array<string>) {
    await this._requestOperator.post(`${this.controller}/CreateRole`, {
      roleName,
      description,
      authorityIds,
    })
  }
  //endregion

  //region # 修改角色 —— updateRole(roleId: string...)
  /**
   * 修改角色
   * @param roleId 角色Id
   * @param roleName 角色名称
   * @param description 描述
   * @param authorityIds 权限Id集
   * */
  public async updateRole(
    roleId: string,
    roleName: string,
    description: string,
    authorityIds: Array<string>,
  ) {
    await this._requestOperator.post(`${this.controller}/UpdateRole`, {
      roleId,
      roleName,
      description,
      authorityIds,
    })
  }
  //endregion

  //region # 删除角色 —— removeRole(roleId: string...)
  /**
   * 删除角色
   * @param roleId 角色Id
   * */
  public async removeRole(roleId: string) {
    await this._requestOperator.post(`${this.controller}/RemoveRole`, {
      roleId,
    })
  }
  //endregion

  //region # 关联权限到角色 —— relateAuthoritiesToRole(roleId: string...)
  /**
   * 关联权限到角色
   * @param roleId 角色Id
   * @param authorityIds 权限Id集
   * */
  public async relateAuthoritiesToRole(roleId: string, authorityIds: Array<string>) {
    await this._requestOperator.post(`${this.controller}/RelateAuthoritiesToRole`, {
      roleId,
      authorityIds,
    })
  }
  //endregion

  //region # 追加权限到角色 —— appendAuthoritiesToRole(roleId: string...)
  /**
   * 追加权限到角色
   * @param roleId 角色Id
   * @param authorityIds 权限Id集
   * */
  public async appendAuthoritiesToRole(roleId: string, authorityIds: Array<string>) {
    await this._requestOperator.post(`${this.controller}/AppendAuthoritiesToRole`, {
      roleId,
      authorityIds,
    })
  }
  //endregion

  //region # 获取权限 —— getAuthority(authorityId: string...)
  /**
   * 获取权限
   * @param authorityId 权限Id
   * */
  public async getAuthority(authorityId: string): Promise<AuthorityInfo> {
    return await this._requestOperator.get(`${this.controller}/GetAuthority`, {
      authorityId,
    })
  }
  //endregion

  //region # 获取权限列表 —— getAuthorities(keywords: string...)
  /**
   * 获取权限列表
   * @param keywords 关键字
   * @param menuId 菜单Id
   * @param roleId 角色Id
   * */
  public async getAuthorities(
    keywords: string | null,
    menuId: string | null,
    roleId: string | null,
  ): Promise<Array<AuthorityInfo>> {
    return await this._requestOperator.get(`${this.controller}/GetAuthorities`, {
      keywords,
      menuId,
      roleId,
    })
  }

  //endregion

  //region # 分页获取权限列表 —— getAuthoritiesByPage(keywords: string...)
  /**
   * 分页获取权限列表
   * @param keywords 关键字
   * @param pageIndex - 页码
   * @param pageSize - 页容量
   * */
  public async getAuthoritiesByPage(
    keywords: string | null,
    pageIndex: number,
    pageSize: number,
  ): Promise<PageModel<AuthorityInfo>> {
    return await this._requestOperator.get(`${this.controller}/GetAuthoritiesByPage`, {
      keywords,
      pageIndex,
      pageSize,
    })
  }

  //endregion

  //region # 获取菜单 —— getMenu(menuId: string...)
  /**
   * 获取菜单
   * @param menuId 菜单Id
   * */
  public async getMenu(menuId: string): Promise<MenuInfo> {
    return await this._requestOperator.get(`${this.controller}/GetMenu`, {
      menuId,
    })
  }
  //endregion

  //region # 获取权限列表 —— getMenus(keywords: string)
  /**
   * 获取权限列表
   * @param keywords 关键字
   * */
  public async getMenus(keywords: string | null): Promise<Array<MenuInfo>> {
    return await this._requestOperator.get(`${this.controller}/GetMenus`, {
      keywords,
    })
  }

  //endregion

  //region # 分页获取菜单列表 —— getMenusByPage(keywords: string...)
  /**
   * 分页获取菜单列表
   * @param keywords 关键字
   * @param pageIndex - 页码
   * @param pageSize - 页容量
   * */
  public async getMenusByPage(
    keywords: string | null,
    pageIndex: number,
    pageSize: number,
  ): Promise<PageModel<MenuInfo>> {
    return await this._requestOperator.get(`${this.controller}/GetMenusByPage`, {
      keywords,
      pageIndex,
      pageSize,
    })
  }

  //endregion

  //region # 获取角色 —— getRole(roleId: string...)
  /**
   * 获取角色
   * @param roleId 角色Id
   * */
  public async getRole(roleId: string): Promise<RoleInfo> {
    return await this._requestOperator.get(`${this.controller}/GetRole`, {
      roleId,
    })
  }

  //endregion

  //region # 获取权限列表 —— getRoles(keywords: string)
  /**
   * 获取权限列表
   * @param keywords 关键字
   * @param loginId 用户名
   * */
  public async getRoles(keywords: string | null, loginId: string | null): Promise<Array<RoleInfo>> {
    return await this._requestOperator.get(`${this.controller}/GetRoles`, {
      keywords,
      loginId,
    })
  }

  //endregion

  //region # 分页获取角色列表 —— getRolesByPage(keywords: string...)
  /**
   * 分页获取角色列表
   * @param keywords 关键字
   * @param pageIndex - 页码
   * @param pageSize - 页容量
   * */
  public async getRolesByPage(
    keywords: string | null,
    pageIndex: number,
    pageSize: number,
  ): Promise<PageModel<RoleInfo>> {
    return await this._requestOperator.get(`${this.controller}/GetRolesByPage`, {
      keywords,
      pageIndex,
      pageSize,
    })
  }

  //endregion

  //region # 是否存在权限 —— existsAuthority(authorityPath: string...)
  /**
   * 是否存在权限
   * @param authorityPath 权限路径
   * */
  public async existsAuthority(authorityPath: string): Promise<boolean> {
    return await this._requestOperator.get(`${this.controller}/ExistsAuthority`, {
      authorityPath,
    })
  }

  //endregion

  //region # 是否存在菜单 —— existsMenu(parentNodeId: string...)
  /**
   * 是否存在菜单
   * @param parentNodeId 上级节点Id
   * @param menuName 菜单名称
   * */
  public async existsMenu(parentNodeId: string, menuName: string): Promise<boolean> {
    return await this._requestOperator.get(`${this.controller}/ExistsMenu`, {
      parentNodeId,
      menuName,
    })
  }

  //endregion

  //region # 是否存在角色 —— existsRole(roleId: string...)
  /**
   * 是否存在角色
   * @param roleId 角色Id
   * @param roleName 角色名称
   * */
  public async existsRole(roleId: string, roleName: string): Promise<boolean> {
    return await this._requestOperator.get(`${this.controller}/ExistsRole`, {
      roleId,
      roleName,
    })
  }

  //endregion
}
